﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Linq.Expressions;
using Common;
using DAL;
using System.Data.Linq;
namespace BAL
{
    public abstract class BaseBAL<T1, T2>
        where T1 : class, IEntity, new()
        where T2 : class, IEntity, new()
    {
        internal static T1 TransT2ToT1(T2 t2)
        {
            T1 t1 = new T1();

            Utility.SetObjectValueWithTheSamePropertyName(t1, t2);

            return t1;
        }
        public static T1 Add(T1 t1)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                T2 t2 = new T2();

                Utility.SetObjectValueWithTheSamePropertyName(t2, t1);

                t2.CreateDate = DateTime.Now;

                DatabaseHelper.Insert<T2>(t2);

                return TransT2ToT1(t2);
            }
        }
        public static IList<T1> Add(IList<T1> t1s)
        {
            for (int i = 0; i < t1s.Count; i++)
            {
                T1 t1 = t1s[i];

                t1 = Add(t1);
            }
            return t1s;
        }

        //根据Id获取T1
        public static T1 GetT1(int Id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransT2ToT1(db.GetTable<T2>().Single(b => b.Id.Equals(Id)));
            }
        }

        //根据Id列表获取T1列表
        public static IList<T1> GetT1s(IList<int> Ids)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<T2>().Where(b => GetArray<int>.Get(Ids).Contains(b.Id)).Select(b => TransT2ToT1(b)).ToList();
            }
        }

        //获取分页数据T1
        public static IList<T1> GetT1s(int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<T2>().Skip(first).Take(max).Select(c => TransT2ToT1(c)).ToList();
            }
        }

        //获取T1总数
        public static int GetT1Count()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<T2>().Count();
            }
        }

        //传入查询条件查询
        public static IQueryable<T2> GetT2IQueryable(Expression<Func<T2, bool>> func, Models.QuesDataContextDataContext db)
        {
            return GetT2IQueryable(func, 0, int.MaxValue, db);
        }

        //传入查询条件查询
        public static IQueryable<T2> GetT2IQueryable(IList<Expression<Func<T2, bool>>> funcs, Models.QuesDataContextDataContext db)
        {
            return GetT2IQueryable(funcs, 0, int.MaxValue, db);
        }

        //传入查询条件查询
        public static IQueryable<T2> GetT2IQueryable(Expression<Func<T2, bool>> func, int first, int max, Models.QuesDataContextDataContext db)
        {
            IList<Expression<Func<T2, bool>>> funcs = new List<Expression<Func<T2, bool>>>();
            funcs.Add(func);
            return GetT2IQueryable(funcs, first, max, db);
        }

        //传入查询条件查询
        public static IQueryable<T2> GetT2IQueryable(IList<Expression<Func<T2, bool>>> funcs, int first, int max, DataContext db)
        {
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            if (funcs.Count > 0)
            {
                IQueryable<T2> iqueryableT2 = db.GetTable<T2>().Where(funcs[0]);
                for (int i = 1; i < funcs.Count; i++)
                {
                    iqueryableT2 = iqueryableT2.Where(funcs[i]);
                }
                return iqueryableT2.Skip(first).Take(max);
            }
            else
                return db.GetTable<T2>().Skip(first).Take(max);
            //}
        }


        //设定查询区间
        public static IQueryable<T2> SetT2IQueryableRange(IQueryable<T2> iqueryableT2, int first, int max)
        {
            iqueryableT2 = iqueryableT2.Skip(first).Take(max);
            return iqueryableT2;
        }

        //根据Id核查存在
        public static bool CheckObjectById(int Id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                var dd = db.GetTable<T2>().Where(w => w.Id == Id);

                return dd.Count() > 0;
            }
        }

        //修改
        public static void Edit(T1 t1)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                T2 t2 = db.GetTable<T2>().Single(b => b.Id.Equals(t1.Id));

                Utility.SetObjectValueWithTheSamePropertyName(t2, t1);

                db.SubmitChanges();
            }
        }

        //批量修改
        public static void Edit(IList<T1> t1s)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                for (int i = 0; i < t1s.Count; i++)
                {
                    T1 t1 = t1s[i];

                    T2 t2 = db.GetTable<T2>().Single(b => b.Id.Equals(t1.Id));

                    Utility.SetObjectValueWithTheSamePropertyName(t2, t1);
                }
                db.SubmitChanges();
            }
        }

        //删除
        public static void Delete(T1 t1)
        {
            Delete(t1.Id);
        }

        //删除
        public static void Delete(int Id)
        {
            IList<int> ids = new List<int>();
            ids.Add(Id);
            Delete(ids);
        }

        //批量删除
        public static void Delete(IList<T1> t1s)
        {
            Delete(t1s.Select(t => t.Id).ToList());
        }

        //批量删除
        public static void Delete(IList<int> ids)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (ids.Count > 0)
                {
                    string sql = string.Format("delete from [{0}] where id in ({1})", typeof(T2).Name, string.Join(",", Common.GetArray<int>.Get(ids).ConvertToStringArray()));

                    db.ExecuteCommand(sql);
                }
            }
        }
    }
}
